package com.tepth.busfix.main.alarm;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tepth.busfix.R;
import com.tepth.busfix.adapter.AlarmListAdapter;
import com.tepth.busfix.base.BaseFragment;
import com.tepth.busfix.bean.reponse.AlarmInfo;

import java.util.ArrayList;

import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * Created by 张宇 on 2017/3/14.
 */

public class AlarmFragment extends BaseFragment implements BaseQuickAdapter.RequestLoadMoreListener,
        SwipeRefreshLayout.OnRefreshListener {
    private String type;
    private ArrayList<AlarmInfo> alarmInfos;
    private RecyclerView recyclerView;
    private AlarmListAdapter alarmListAdapter;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    SweetAlertDialog pDialog;
    private boolean isPortrait;


    public static AlarmFragment newInstance(String type, ArrayList<AlarmInfo> alarmInfos,
                                            boolean isPortrait) {
        Bundle arguments = new Bundle();
        arguments.putString("type", type);
        arguments.putSerializable("list", alarmInfos);
        arguments.putBoolean("isPortrait", isPortrait);
        AlarmFragment fragment = new AlarmFragment();
        fragment.setArguments(arguments);
        return fragment;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab_layout, null);
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeLayout);
        mSwipeRefreshLayout.setOnRefreshListener(this);
        if (isPortrait) {
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        } else {
            recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        }

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        type = getArguments().getString("type");
        alarmInfos = (ArrayList<AlarmInfo>) getArguments().getSerializable("list");
        isPortrait = getArguments().getBoolean("isPortrait");
        alarmListAdapter = new AlarmListAdapter(getActivity(), R.layout.item_alarm, alarmInfos);
        recyclerView.setAdapter(alarmListAdapter);
    }

    @Override
    public void onRefresh() {
        mSwipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void onLoadMoreRequested() {

    }

    @Override
    public void onChange(boolean isPor) {
        if(recyclerView == null){
            return;
        }
        if (isPor) {
            recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        } else {
            recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
        }
    }
}
